#pragma once

#include <map>

namespace fxcc
{

    template<typename T>
    struct _PtrMap
    {
        typename T::Desc m_Desc;
        T* m_Value{ 0 };

    };
    template<typename T>
    struct ResourceMap
    {
        std::map<typename T::Desc, std::shared_ptr<T>> m_DataList;

        void ProcessPtr(std::function<void(const typename T::Desc&, T)> func)
        {
            for (const auto& [key, value] : m_DataList)
            {
                func(key, value);
            }
        }
        bool Load(const typename T::Desc& desc)
        {
            if (Has(desc))
            {
                return true;
            }

            auto inst = std::make_shared<T>(desc);

            if (inst->m_Available)
            {
                m_DataList[desc] = inst;
            }
            return inst->m_Available;
        }

        bool Has(const typename T::Desc& desc)
        {
            auto it = m_DataList.find(desc);
            return it != m_DataList.end();

        }

        std::shared_ptr<T> Get(const typename T::Desc& desc)
        {
            auto it = m_DataList.find(desc);
            return it->second;
        }
//
        bool LoadMap( _PtrMap<T>& mp)
        {
            const auto& desc = mp.m_Desc;

            if (!Has(desc))
            {
                Load(desc);
            }
            if (Has(desc))
            {
                mp.m_Value = Get(desc).get();
            }
            return false;

        }

        //static ResourceMap& Inst()
        //{
        //    static ResourceMap res;
        //    return res;
        //}
    };

};